package com.ykt.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.ykt.common.utils.SqlUtil;

import java.util.List;
import java.util.Map;

/**
 * 多媒体资源
 * @author long.hua
 * @version 1.0.0
 * @since 2016-05-24 23:36
 */
public class MediaSource extends BaseModel<MediaSource>{

    public static String biz_type = "biz_type";
    public static String biz_sub_type = "biz_sub_type";
    public static String biz_id = "biz_id";
    public static String media_type = "media_type";
    public static String cover = "cover";
    public static String content = "content";
    public static String sort = "sort";
    public static String status = "status";


    public static final MediaSource dao = new MediaSource();


    public enum BizType{
        /**市场*/
        MARKET,
        /**商铺*/
        SHOP,
        /**商品*/
        GOODS
    }

    public enum BizSubType{
        /**商品头部需展示的资源*/
        GOODS_HEAD,
        /**商品详情需展示的资源*/
        GOODS_DETAIL
    }

    public enum MediaType{
        IMAGE,
        VIDEO,
        LIVE
    }
    
    @Override
    public Page<MediaSource> pageFindModel(int pageNumber, int rows, Map<String, String> paramMap) {
        String select = "SELECT m.id marketId, m.name marketName, s.id shopId, s.name shopName, g.id goodsId, g.name goodsName,"
        		+ " ms.id, ms.media_type, ms.cover, ms.content, ms.sort,"
        		+ " c.device_name liveName, c.status liveStatus, v.title videoName, v.vid ";
        String sqlExceptSelect = " FROM media_source ms "
				+ " LEFT JOIN market m ON (ms.biz_id = m.id AND ms.biz_type='MARKET') "
        		+ " LEFT JOIN shop s ON (ms.biz_id = s.id AND ms.biz_type='SHOP') "
        		+ " LEFT JOIN goods g ON (ms.biz_id = g.id AND ms.biz_type='GOODS') "
        		+ " LEFT JOIN camera c ON (ms.content = c.id AND ms.media_type='LIVE') "
        		+ " LEFT JOIN video v ON (ms.content = v.vid AND ms.media_type='VIDEO') ";

        paramMap.put("EQ_ms.biz_type", paramMap.get("ms.biz_type"));
        paramMap.put("EQ_ms.biz_id", paramMap.get("ms.biz_id"));
        
        String sqlOrderBy = " ORDER BY ms.sort ";
        paramMap.put(OB, sqlOrderBy);

        return this.pageFindModel(select, sqlExceptSelect, pageNumber, rows, paramMap);
    }
    
    /**
     * 删除和业务ID相关的媒体资源
     */
    public void deleteMediaSource(int bizId, BizType bizType, BizSubType bizSubType){
    	String sql = "delete from media_source where status=1 and biz_id=? and biz_type=? ";
    	if(bizSubType==null){
    		Db.update(sql, bizId, bizType.name());
    	}else{
    		sql += " and biz_sub_type=? ";
    		Db.update(sql, bizId, bizType.name(), bizSubType.name());
    	}
    }

    /**
     * 查询有效的业务ID的多媒体资源
     */
    public List<MediaSource> find(BizType bizType, int bizId){
        String sql = "select * from media_source where biz_type=? and biz_id=? and status=1 order by sort";
        return find(sql, bizType.name(), bizId);
    }


    /**
     * 查询有效的一批业务ID的多媒体资源
     */
    public List<MediaSource> find(BizType bizType, List<Integer> bizIds){
        String inVals = SqlUtil.fillPlaceholder(bizIds);

        String sql = "select *" +
                " from media_source" +
                " where biz_type=?" +
                " and biz_id in (" + inVals + ")" +
                " and status=1" +
                " order by sort";

        return find(sql, bizType.name());
    }
}
